package org.example.chatroom.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.chatroom.common.pojo.dataobject.DialogueUser;
import org.example.chatroom.common.pojo.dataobject.Message;
import org.example.chatroom.common.pojo.dataobject.UserInfo;
import org.example.chatroom.common.pojo.response.MessageResponse;
import org.example.chatroom.common.pojo.response.WebSocketMessageResponse;
import org.example.chatroom.mapper.DialogueUserMapper;
import org.example.chatroom.mapper.MessageMapper;
import org.example.chatroom.mapper.UserMapper;
import org.example.chatroom.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-06-26
 * Time: 16:05
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private DialogueUserMapper dialogueUserMapper;

    @Override
    public List<MessageResponse> getmessage(Integer dialogueId) {
        //查找用户id 发送内容 发送时间
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Message::getDialogueId,dialogueId)
                .orderByAsc(Message::getSendTime);
        List<Message> messages = messageMapper.selectList(queryWrapper);
        List<MessageResponse> messageResponses =new ArrayList<>();
        for(Message message : messages){
            QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
            userInfoQueryWrapper.lambda().eq(UserInfo::getId,message.getUserId());
            UserInfo userInfo = userMapper.selectOne(userInfoQueryWrapper);
            messageResponses.add(new MessageResponse(userInfo.getId(),userInfo.getUserName(),userInfo.getAvatarUrl(),
                    message.getContent(),message.getSendTime()));
        }
        return messageResponses;
    }

    @Override
    public void add(Message message) {
        messageMapper.insert(message);
    }

    @Override
    public List<WebSocketMessageResponse> getOfflineMessageRequests(Integer userId, LocalDateTime offlineTime) {

        // 1. 获取用户参与的所有对话
        QueryWrapper<DialogueUser> dialogueUserQuery = new QueryWrapper<>();
        dialogueUserQuery.lambda().eq(DialogueUser::getUserId, userId);
        List<DialogueUser> dialogueUsers = dialogueUserMapper.selectList(dialogueUserQuery);
        List<Integer> dialogueIds = dialogueUsers.stream()
                .map(DialogueUser::getDialogueId)
                .collect(Collectors.toList());

        if (dialogueIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 2. 查询这些对话中的离线消息
        QueryWrapper<Message> messageQuery = new QueryWrapper<>();
        messageQuery.lambda()
                .in(Message::getDialogueId, dialogueIds)
                .gt(Message::getSendTime, offlineTime)
                .ne(Message::getUserId, userId) // 排除自己发送的消息
                .orderByAsc(Message::getSendTime);

        List<Message> messages = messageMapper.selectList(messageQuery);

        // 3. 转换为响应对象
        return messages.stream().map(message -> {
            QueryWrapper<UserInfo> userInfoQuery = new QueryWrapper<>();
            userInfoQuery.lambda()
                    .eq(UserInfo::getId, message.getUserId())
                    .eq(UserInfo::getDeleteFlag, 0);
            UserInfo userInfo = userMapper.selectOne(userInfoQuery);

            return new WebSocketMessageResponse(
                    "message",
                    userInfo.getId(),
                    userInfo.getUserName(),
                    message.getDialogueId(),
                    message.getContent()
            );
        }).collect(Collectors.toList());
    }
}
